{extend name="public:base" /}
{block name="css"}<link rel="stylesheet" href="/static/admin/tree/css/treeTable.css">{/block}
{block name="css"}
<style type="text/css">
.layui-table-body, .layui-table-box, .layui-table-cell{
overflow: visible;
}
 
/* 设置下拉框的高度与表格单元相同 */
 td .layui-table-cell .layui-form-select {
	margin-top: -10px;
	margin-left: -15px;
	margin-right: -15px;
}
</style>
{/block}

{block name="body"}

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-header layuiadmin-card-header-auto">
	      {if condition="checkRuleButton('forum/addtags')"}
        <button class="layui-btn layuiadmin-btn-tags" data-type="add">添加</button>
		    {/if}
      </div>

      <div class="page-wrapper">
        <div class="demo-side">
            <table id="Article-cate-list"></table>
        </div>
      </div>

      <div class="layui-card-body">
        <script type="text/html" id="buttonHot">
          {if condition="checkRuleButton('Forum/tagshot')"}<input type="checkbox" name="is_hot" lay-skin="primary" lay-filter="menu-show" {{# if(d.is_hot ==1){ }} checked value="0"{{#  } else { }}value="1"{{#  } }} id="{{d.id}}" >
          {else /}<input type="checkbox" title="禁用" disabled> {/if}
          </script>
          <script type="text/html" id="layuiadmin-app-cont-tagsbar">
          {if condition="checkRuleButton('forum/tagsform')"}
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
          {else /}<a class="layui-btn layui-btn-disabled layui-btn-normal layui-btn-xs"><i class="layui-icon layui-icon-edit"></i></a>{/if}
          {if condition="checkRuleButton('forum/tagsdelete')"} 
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
          {else /}<a class="layui-btn layui-btn-disabled layui-btn-danger layui-btn-xs"><i class="layui-icon layui-icon-delete"></i></a>{/if}
        </script>
      </div>
    </div>
  </div>

{/block}

{block name="js"}
<script>
  var forumList = "{:url('Forum/list')}",
      forumListdel = "{:url('Forum/listdel')}",
      forumListform = "{:url('Forum/listform')}",
      forumReplys = "{:url('Forum/replys')}",
      forumRedel = "{:url('Forum/redel')}",
      forumReplysform = "{:url('Forum/replysform')}",
      forumTags = "{:url('Forum/tags')}",
      forumTagsDelete = "{:url('Forum/tagsdelete')}",
      forumTagsForm = "{:url('Forum/tagsform')}";

  layui.config({
    base: '/static/admin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','treeTable'], function(){

    var table = layui.table, form = layui.form;
    var $ = layui.$,treeTable = layui.treeTable;

    // 渲染分类表格
    var insTb = treeTable.render({
      elem: '#Article-cate-list',
      url: forumTags,
      //toolbar: 'default',
      //height: 'full-200',
      tree: {
          iconIndex: 2,
          isPidData: true,
          idName: 'id',
          pidName: 'pid'
      },
      defaultToolbar: ['filter', 'print', 'exports'],
      cols: [
          [
            {type: 'numbers'},
            {type: 'checkbox'}
            ,{field: 'catename', title: '分类名', width: 150}
            ,{field: 'appname', title: '所属应用', width: 90}
            ,{field: 'ename', title: 'EN别名', width: 100}
            ,{field: 'detpl',title: '模板', align: 'center',width: 100}
            ,{title: '图标', align: 'center',width: 50,templet: '<p><i class="layui-icon {{d.icon}}"></i></p>'}
            ,{field: 'is_hot', title: '热门', align: 'center',width: 50, templet: '#buttonHot'}
            ,{field: 'desc', title: '描述', minWidth: 200}
            ,{field: 'id', title: 'ID',width: 60}
            ,{field: 'sort', title: '排序', width: 80, sort: true}
            ,{title: '操作', width: 120, align: 'center', toolbar: '#layuiadmin-app-cont-tagsbar'}
          ]
      ],
      style: 'margin-top:0;'
  });

    //监听工具条
    treeTable.on('tool(Article-cate-list)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('是否删除此分类及分类下内容？', function(index){
                $.ajax({
                    type:'post',
                    url:forumTagsDelete,
                    data:{id:data.id},
                    dataType:'json',
                    success:function(data){
                        if(data.code == 0){layer.msg(data.msg,{icon:6,time:2000},function(){
                                location.reload();
                            });
                        } else {
                            layer.open({title:'删除失败',content:data.msg,icon:5,adim:6})
                        }
                    }
                });
                layer.close(index);
            });
        } else if(obj.event === 'edit'){
            var tr = $(obj.tr);
            layer.open({
                type: 2
                ,title: '编辑分类'
                ,content: forumTagsForm + '?id='+ data.id
                ,area: ['400px', '500px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //获取iframe元素的值
                    var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                        ,pid = othis.find('input[name="pid"]').val()
                        ,sort = othis.find('input[name="sort"]').val()
                        ,appname = othis.find('select[name="appname"]').val()
                        ,catename = othis.find('input[name="catename"]').val()
                        ,ename = othis.find('input[name="ename"]').val()
                        ,detpl = othis.find('select[name="detpl"]').val()
                        ,icon = othis.find('input[name="icon"]').val()
                        ,desc = othis.find('input[name="desc"]').val();

                    if(!catename.replace(/\s/g, '')) return;

                    $.ajax({
                        type:"post",
                        url:forumTagsForm,
                        data:{"id":data.id,"pid":pid,"sort":sort,"appname":appname,"catename":catename,"ename":ename,"detpl":detpl,"icon":icon,"desc":desc},
                        daType:"json",
                        success:function (data){
                          if(data.code == 0){layer.msg(data.msg,{icon:6,time:2000},function(){
                                location.reload();
                            });
                        } else {
                            layer.open({title:'修改失败',content:data.msg,icon:5,adim:6})
                        }
                        }
                    });
                    /*
                              obj.update({
                                tags: tags
                                ,ename: ename
                                ,sort: sort
                              });
                    */
                    treeTable.reload('Article-cate-list'); //数据刷新
                    layer.close(index);
                }
                ,success: function(layero, index){
                    //给iframe元素赋值
                    var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags").click();
                        othis.find('input[name="pid"]').val(data.pid)
                        ,othis.find('input[name="sort"]').val(data.sort)
                        ,othis.find('select[name="appname"]').val(data.appname)
                        ,othis.find('input[name="catename"]').val(data.catename)
                        ,othis.find('input[name="ename"]').val(data.ename)
                        ,othis.find('input[name="icon"]').val(data.icon)
                        ,othis.find('input[name="desc"]').val(data.desc);
                }
            });
        }
    });
    
    var active = {
      add: function(){
        layer.open({
          type: 2
          ,title: '添加分类'
          ,content: 'tagsform.html'
          ,area: ['450px', '550px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                    ,pid = othis.find('input[name="pid"]').val()
                    ,sort = othis.find('input[name="sort"]').val()
                    ,appname = othis.find('select[name="appname"]').val()
                    ,catename = othis.find('input[name="catename"]').val()
                    ,ename = othis.find('input[name="ename"]').val()
                    ,detpl = othis.find('select[name="detpl"]').val()
                    ,icon = othis.find('input[name="icon"]').val()
                    ,desc = othis.find('input[name="desc"]').val();
            
            if(!catename.replace(/\s/g, '')) return;
			
        $.ajax({
				type:"post",
				url:"{:url('Forum/tagsform')}",
				data:{pid:pid,"sort":sort,"appname":appname,"catename":catename,"ename":ename,"detpl":detpl,"icon":icon,"desc":desc},
				daType:"json",
				success:function (data){
					if (data.code == 0) {
						layer.msg(data.msg,{icon:6,time:2000});
					} else {
						layer.open({title:'添加失败',content:data.msg,icon:5,anim:6});
					}
				}
			});
            table.reload('LAY-app-content-tags');
            layer.close(index);
          }
        }); 
      }
    }

    //分类热点控制
    form.on('checkbox(menu-show)', function(data){
    var data = data.elem;
    data.value = data.checked ? 1 : 0;
    $.ajax({
        type:'post',
        url:"{:url('Forum/tagshot')}",
        data:{"id":data.id,"is_hot":data.value,},
        dataType:'json',
        success:function(data){
          if(data.code == 0){
            layer.msg(data.msg,{icon:6,time:2000});
          } else {
            layer.open({title:'修改失败',content:data.msg,icon:5,adim:6});
          }
        }
      });
    return false;
    });

	
    $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  });
</script>
{/block}
